//
//  LBUpdateBatchViewController.m
//  TEXT
//
//  Created by mac on 2025/1/4.
//  Copyright © 2025 刘博. All rights reserved.
//

#import "LBUpdateBatchViewController.h"
#import "PerformBatchCell.h"

@interface LBUpdateBatchViewController () <UICollectionViewDelegate, UICollectionViewDataSource, UICollectionViewDelegateFlowLayout>

@property (nonatomic, strong) UICollectionView *collectionView;

@property (nonatomic, strong) UIButton *firstButton;
@property (nonatomic, strong) UIButton *secondButton;

@property (nonatomic, strong) UIButton *thirdButton;

@property (nonatomic, assign) NSInteger flag;

@property (nonatomic, assign) NSInteger itemCount;

@end

@implementation LBUpdateBatchViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.view.backgroundColor = [UIColor whiteColor];
    self.flag = 1;
    self.itemCount = 20;
    [self.view addSubview:self.collectionView];
    [self.view addSubview:self.firstButton];
    [self.view addSubview:self.secondButton];
    [self.view addSubview:self.thirdButton];
    int list[9] = {12, 3, 10, 8, 9, 7, 6, 11, 16};

    int median = findMedian(list, 9);
    printf("the median is %d \n", median);
    // Do any additional setup after loading the view.
}

- (void)firstClick
{
    int list[9] = {12, 3, 10, 8, 9, 7, 6, 11, 16};

    int median = findMedian(list, 9);
    printf("the median is %d \n", median);
//    self.flag = 0;
//    [self.collectionView reloadData];
}

- (void)secondClick
{
    self.flag = 2;
    [self.collectionView performBatchUpdates:nil completion:nil];
}

- (void)loadPreviousPage
{
    self.itemCount += 6;
    NSMutableArray *array = [NSMutableArray array];
    for (int i = 0; i < 6; i ++) {
        NSIndexPath *indexPath = [NSIndexPath indexPathForItem:i inSection:0];
        [array addObject:indexPath];
    }
    [CATransaction setDisableActions:YES];
    [self.collectionView performBatchUpdates:^{
        [self.collectionView insertItemsAtIndexPaths:array];
        } completion:^(BOOL finished) {
            [CATransaction setDisableActions:NO];
            if (finished) {
                CGRect frame = [self.collectionView layoutAttributesForItemAtIndexPath:[NSIndexPath indexPathForItem:6 inSection:0]].frame;
                [self.collectionView setContentOffset:CGPointMake(0, frame.origin.y - 100)];
                self.flag --;
                if (self.flag <= 0) {
                    ///注意，这里一定要用 performBatchUpdates 不能用reloadData
                    [self.collectionView performBatchUpdates:nil completion:nil];
                }
            }
        }];

}

int findMedian(int a[], int aLen) {
    int low = 0;
    int high = aLen -1;
    
    int mid = (aLen - 1) / 2;
    int div = PartSort(a, low, high);
    
    while (div != mid) {
        if (mid < div) {
            // 左半区间找
            div = PartSort(a, low, div - 1);
        }
        else {
            // 右半区间找
            div = PartSort(a, div + 1, high);
        }
    }
   

    return a[mid];
}

int PartSort(int a[], int start, int end) {
    int low = start;
    int high = end;
    
    // 选取关键字
    int key = a[end];
    NSLog(@"阿哈哈哈这里这里key= %d",key);
    while (low < high) {
        // 左边找比key大的值
        while (low < high && a[low] <= key) {
            ++low;
        }
        
        // 右边找比key小的值
        while (low < high && a[high] >= key) {
            --high;
        }
        
        if (low < high) {
            // 找到之后交换左右的值
            int temp = a[low];
            a[low] = a[high];
            a[high] = temp;
        }
    }
    for (int i = 0; i <= 8; i ++) {
        NSLog(@"啦啦啦啦该改革之后最终的%d", a[i]);
    }
    int temp = a[high];
    a[high] = a[end];
    a[end] = temp;
    
   
    NSLog(@"\n\n\n");
    for (int i = 0; i <= 8; i ++) {
        NSLog(@"啦啦啦啦该改革之后最终的%d", a[i]);
    }
    
    NSLog(@"\n\n\n ");
    NSLog(@"\n\n\n ");

    return low;
}

- (void)kk {
    int list[9] = {12, 3, 10, 8, 9, 7, 6, 11, 16};

}

#pragma mark - UICollectionViewDelegate, UICollectionViewDataSource

- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{
    return self.itemCount;
}

- (UICollectionReusableView *)collectionView:(UICollectionView *)collectionView viewForSupplementaryElementOfKind:(NSString *)kind atIndexPath:(NSIndexPath *)indexPath
{
    if ([kind isEqualToString:UICollectionElementKindSectionHeader]) {
        UICollectionReusableView *header = [collectionView dequeueReusableSupplementaryViewOfKind:kind withReuseIdentifier:@"header" forIndexPath:indexPath];
        header.backgroundColor = [UIColor cyanColor];
        return header;
    }
    return nil;
}

- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout referenceSizeForHeaderInSection:(NSInteger)section
{
    CGFloat height = 0;
    if (self.flag >  0) {
        height = 100;
    }
    return CGSizeMake(300, height);
}


- (__kindof UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
    PerformBatchCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"cell" forIndexPath:indexPath];
    NSString *title = [NSString stringWithFormat:@"%ld", indexPath.item];
    [cell updateWithText:title];
    return cell;
}

- (UICollectionView *)collectionView
{
    if (!_collectionView) {
        UICollectionViewFlowLayout *layout = [[UICollectionViewFlowLayout alloc] init];
        layout.itemSize = CGSizeMake(100, 100);
        layout.minimumLineSpacing = 10;
        layout.minimumInteritemSpacing = 10;
        _collectionView = [[UICollectionView alloc] initWithFrame:CGRectMake(10, 100, 300, 600) collectionViewLayout:layout];
        [_collectionView registerClass:[PerformBatchCell class] forCellWithReuseIdentifier:@"cell"];
        [_collectionView registerClass:[UICollectionReusableView class] forSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:@"header"];
        _collectionView.delegate = self;
        _collectionView.dataSource = self;
    }
    return _collectionView;
}

- (UIButton *)firstButton
{
    if (!_firstButton) {
        _firstButton = [UIButton buttonWithType:UIButtonTypeCustom];
        _firstButton.frame = CGRectMake(100, 100, 200, 100);
        _firstButton.titleLabel.font = [UIFont systemFontOfSize:14];
        _firstButton.backgroundColor = [UIColor redColor];
        [_firstButton setTitle:@"隐藏头部并reloadData" forState:UIControlStateNormal];
        [_firstButton addTarget:self action:@selector(firstClick) forControlEvents:UIControlEventTouchUpInside];
    }
    return _firstButton;
}

- (UIButton *)secondButton
{
    if (!_secondButton) {
        _secondButton = [UIButton buttonWithType:UIButtonTypeCustom];
        _secondButton.frame = CGRectMake(100, 250, 200, 100);
        _secondButton.backgroundColor = [UIColor greenColor];
        [_secondButton setTitle:@"展示头部并performBatchUpdates" forState:UIControlStateNormal];
        [_secondButton addTarget:self action:@selector(secondClick) forControlEvents:UIControlEventTouchUpInside];
        _secondButton.titleLabel.font = [UIFont systemFontOfSize:14];

    }
    return _secondButton;
}

- (UIButton *)thirdButton
{
    if (!_thirdButton) {
        _thirdButton = [UIButton buttonWithType:UIButtonTypeCustom];
        _thirdButton.frame = CGRectMake(100, 400, 200, 100);
        _thirdButton.backgroundColor = [UIColor blueColor];
        [_thirdButton setTitle:@"模拟加载上一页" forState:UIControlStateNormal];
        [_thirdButton addTarget:self action:@selector(loadPreviousPage) forControlEvents:UIControlEventTouchUpInside];
        _thirdButton.titleLabel.font = [UIFont systemFontOfSize:14];
    }
    return _thirdButton;
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
